const util = require('../../../utils/util.js');
var user = require('../../../services/user.js');
const qiniuUploader = require("../../../utils/qiniuUploader");
const Toast = require('../../../utils/toast.js');
// pages/mine/complain.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lessonname: '',
    lessonprice: 0,
    lessontime: ['1小时内', '1-2小时', '2小时以上'],
    lessontimeindex: 0,
    startdate: '',
    enddate: '',
    selectdate: '',
    selecttime: '00:00',
    bannerimg: '',
    infoimg: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let nowtimestamp = Date.now();
    let endtimestamp = Date.now() + 86400 * 30000;
    let date = util.format(nowtimestamp, 'yyyy-MM-dd');
    let enddate = util.format(endtimestamp, 'yyyy-MM-dd');
    //console.log(enddate)
    this.setData({
      startdate: date
    });

    this.setData({
      selectdate: date
    });

    this.setData({
      enddate: enddate
    });
    //执行设置七牛参数
    this.setqiniuoption();

  },
  lessontimechange: function(e) {
    this.setData({
      lessontimeindex: e.detail.value
    })
  },
  datechange: function(e) {
    this.setData({
      selectdate: e.detail.value
    })
  },
  timechange: function(e) {
    this.setData({
      selecttime: e.detail.value
    })
  },
  inputlessonname: function(e) {
    this.setData({
      lessonname: e.detail.value
    })
  },
  inputprice: function(e) {
    this.setData({
      lessonprice: e.detail.value
    })
  },
  setqiniuoption: function() {
    let that = this;
    util.request('upload/getqiniutoken', 'GET').then(function(res) {
      //console.log(res.data);
      if (res.errno === 0) {
        //初始化七牛相关参数
        var options = {
          region: 'ECN', // 华东区
          //uptokenURL: util.apiurl+'upload/getqiniutoken',
          uptoken: res.data,
          domain: 'http://teachimg.178mr.com',
          shouldUseQiniuFileName: false
        };
        qiniuUploader.init(options);
      }
    });
  },
  changebanner: function() {
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        var filePath = res.tempFilePaths[0];
        //交给七牛上传
        qiniuUploader.upload(filePath, (res) => {
            that.setData({
              bannerimg: res.imageURL
            })
          }, (error) => {
            console.error('error: ' + JSON.stringify(error));
          }, {
            region: 'ECN', // 华东区
            key: 'images/livesound/banner' + Date.now()
          },
          (progress) => {
            //console.log('上传进度', progress.progress)
            // console.log('已经上传的数据长度', progress.totalBytesSent)
            // console.log('预期需要上传的数据总长度', progress.totalBytesExpectedToSend)
          }, cancelTask => that.setData({
            cancelTask
          })
        );
      }
    })
  },
  changeinfoimg: function() {
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        var filePath = res.tempFilePaths[0];
        //交给七牛上传
        qiniuUploader.upload(filePath, (res) => {
            that.setData({
              infoimg: res.imageURL
            })
          }, (error) => {
            console.error('error: ' + JSON.stringify(error));
          }, {
            region: 'ECN', // 华东区
            key: 'images/livesound/infoimg' + Date.now()
          },
          (progress) => {
            //console.log('上传进度', progress.progress)
            // console.log('已经上传的数据长度', progress.totalBytesSent)
            // console.log('预期需要上传的数据总长度', progress.totalBytesExpectedToSend)
          }, cancelTask => that.setData({
            cancelTask
          })
        );
      }
    })
  },
  postdata: function() {
    //数据过滤 和 判断
    let that = this;
    if (that.data.lessonname.trim() === '') {
      Toast('请输入课程名称');
      return
    }
    if (!that.data.lessonprice) {
      Toast('请输入价格');
      return
    }
    var selectdate = this.data.selectdate.replace(/\-/g, "/")
    var starttime = selectdate + ' ' + this.data.selecttime;
    var starttimestamp = Math.round(new Date(starttime).getTime() / 1000);
    //console.log(starttimestamp);
    if (!starttimestamp){
      Toast('请选择开课日期');
      return
    }
    if (that.data.selecttime === '00:00') {
      Toast('请选择开课时间');
      return
    }
    if (!that.data.bannerimg) {
      Toast('请上传banner图');
      return
    }
    if (!that.data.infoimg) {
      Toast('请上传详情图');
      return
    }
    var data = {
      lesson_name: this.data.lessonname,
      lesson_price: this.data.lessonprice * 100,
      lesson_duration: this.data.lessontime[this.data.lessontimeindex],
      lesson_starttime: starttimestamp,
      lesson_img: this.data.bannerimg,
      lesson_infoimg: this.data.infoimg,
      addtime: Math.round(Date.now() / 1000)
    }
    //console.log(data);
    util.request('user/postaddlesson', {
      addinfo: data
    }, 'POST').then(function(res) {
      console.log(res.data);
      if (res.errno === 0) {
        wx.navigateBack({
          delta: 1
        })
      }
    });
  }

})